package su.FeatureSelection;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import su.Ner.U;

public class FeatureStore {

	public Hashtable<String, Integer> featureCount;
	public Hashtable<String, Integer> noIdfeatureCount;
	public Hashtable<String, Integer> predicateCount;
	public Hashtable<String, Integer> YCount;
	public Hashtable<String, String> featureWithId;
	
	public Hashtable<String, Double> featurePrecision;
	public Hashtable<String, Double> featureRecall;
	public Hashtable<String, Double> featureFMeasure;
	
	int totalCount = 0;
	
	public static int getFeatureY(String feature) {
		String l = feature.substring(feature.lastIndexOf(':') + 1);
		return Integer.parseInt(l);
	}

	@SuppressWarnings("unchecked")
	public FeatureStore(Hashtable<String, Integer> featureCountTable,Hashtable<String, Integer> _YCount) {

		//loadFeatureNameSet();

		noIdfeatureCount = new Hashtable<String, Integer>();
		YCount=_YCount;
		featureCount = featureCountTable;
		for (String f : featureCountTable.keySet()) {
			noIdfeatureCount.put(U.removeId(f), featureCountTable.get(f));
		}

		featurePrecision = new Hashtable<String, Double>();
		featureRecall = new Hashtable<String, Double>();
		predicateCount = new Hashtable<String, Integer>();
		featureWithId = new Hashtable<String, String>();
		Iterator it = featureCountTable.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String feature = entry.getKey().toString();
			int value = (Integer) entry.getValue();

			featurePrecision.put(feature, (double) value);
			featureRecall.put(feature, (double) value);
			totalCount += value;
			int firstColon = feature.indexOf(':');
			int lastColon = feature.lastIndexOf(':');
			String key = feature.substring(0, firstColon);
			String label = feature.substring(lastColon + 1, feature.length());
			featureWithId.put(key + ":" + label, feature);

			int c = 0;
			if (null != predicateCount.get(key))
				c = (Integer) predicateCount.get(key);
			predicateCount.put(key, c + value);
		}

		// count precision
		for (String key : featurePrecision.keySet()) {
			String x = key.substring(0, key.indexOf(":"));
			featurePrecision.put(key, featurePrecision.get(key) / predicateCount.get(x));
		}
		
		//count recall
		for (String key : featureRecall.keySet()) {
			String l = key.substring( key.lastIndexOf(":")+1,key.length());
			featureRecall.put(key, featureRecall.get(key) / YCount.get(l));
		}

		//f measure
		featureFMeasure=new Hashtable<String, Double>();
		for (String key : featureRecall.keySet()) {
			double p=featurePrecision.get(key);
			double r=featureRecall.get(key);
			featureFMeasure.put(key, 2*p*r/(p+r));
		}
		
	}

	public static double tryGetD(Hashtable<String, Double> ht, String key) {
		if (null != ht.get(key)) {
			return ht.get(key);
		}
		return 0;
	}

	public static float tryGet(HashMap<String, Float> ht, String key) {
		if (null != ht.get(key)) {
			return ht.get(key);
		}
		return 0;
	}
	
	public static int tryGetI(Hashtable<String, Integer> ht, String key) {
		if (null != ht.get(key)) {
			return ht.get(key);
		}
		return 0;
	}


}
